﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using OptusMobile.Web.ServerComponents.BAL.PlanManagement;
using OptusMobile.Web.ServerComponents.BAL.HandsetManagement;
using OptusMobile.Web.ServerComponents.DAL;
using System.Xml;

namespace OptusMobile.Web.Handset
{
    public partial class BuyHandset : System.Web.UI.Page
    {
        private PlanManager planManager;
        private HandsetManager handsetManager;
        private XmlDocument xmldoc;
        public BuyHandset()
        {
            planManager = new PlanManager();
            handsetManager = new HandsetManager();
            xmldoc = new XmlDocument();
        }
        
        protected void Page_Load(object sender, EventArgs e)
        {
            string cssPath = "../css/BuyHandset.css";
            HtmlLink link = new HtmlLink();
            link.Href = cssPath;
            link.Attributes["rel"] = "stylesheet";
            link.Attributes["type"] = "text/css";
            PlaceHolder1.Controls.Add(link);
            try
            {
                string handsetID = Request.QueryString["handsetID"].ToString();
                ShowHandsetImage(handsetID);
                ShowHandsetAllPlan(handsetID);
                //ShowHandsetImage("HSBI100429000001");
                //ShowHandsetAllPlan("HSBI100429000001");
                if (Session["item"] != null)
                {
                    lblItemCount.Text = "Item (" + Session["item"].ToString() + ")";

                }
                else
                {
                    lblItemCount.Text = "Item (0)";
                    Session["item"] = "0";
                }
            }
            catch
            {
                Response.Redirect("AllHandsetShow.aspx");
            }

        }

        protected void ShowHandsetImage(string HandsetID)
        {
            var img = handsetManager.GetHandsetImage().Single(p => p.handsetID == HandsetID);
            btnhandsetImage.ImageUrl =".."+ img.imagePath + img.imageName;
            btnhandsetImage.PostBackUrl = "ShowHandset.aspx?imageID=" + img.imageID.ToString() + "&handsetID=" + HandsetID;
            btnhandsetImage.AlternateText = HandsetID;
        }

        protected void ShowHandsetAllPlan(string HandsetID)
        {
            var plan = planManager.ReadAllCapDetails().Where(p => p.handsetID == HandsetID).First();
            lblPlan.Text = plan.planName;
            lblHandset.Text = plan.brandName + "  " + plan.handsetModole;
            
            string planimage = lblPlan.Text.Substring(0, 2);
            btnplanImage.ImageUrl = "../Image/Plan/"+planimage+"_cap_plan.gif";
            btnplanImage.PostBackUrl = "../ShowAllPlan.aspx";
            btnplanImage.AlternateText = plan.planName;

        }

        protected void linkbtnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("AllHandsetShow.aspx");
        }

        protected void btnAdditem_Click(object sender, EventArgs e)
        {
            xmldoc.Load(Server.MapPath("../Handset/ShoppingCarXML.xml"));
            XmlNode xmldocSelect = xmldoc.SelectSingleNode("item");

            XmlElement el = xmldoc.CreateElement("user"); 
            el.SetAttribute("ID", Session.SessionID.ToString ()); 


            XmlElement xesub1 = xmldoc.CreateElement("handsetID"); 
            xesub1.InnerText = btnhandsetImage.AlternateText.ToString();
            el.AppendChild(xesub1);
            XmlElement xesub2 = xmldoc.CreateElement("handsetURL");
            xesub2.InnerText = btnhandsetImage.ImageUrl.ToString();
            el.AppendChild(xesub2);

            XmlElement xesub3 = xmldoc.CreateElement("planName");
            xesub3.InnerText = btnplanImage.AlternateText.ToString();
            el.AppendChild(xesub3);
            XmlElement xesub4 = xmldoc.CreateElement("planURL");
            xesub4.InnerText = btnplanImage.ImageUrl.ToString();
            el.AppendChild(xesub4);

            XmlElement xesub5 = xmldoc.CreateElement("handsetBrandmodle");
            xesub5.InnerText = lblHandset.Text;
            el.AppendChild(xesub5);
          
            xmldocSelect.AppendChild(el);
            xmldoc.Save(Server.MapPath("../Handset/ShoppingCarXML.xml"));

            Session["item"] = Convert.ToInt32(Session["item"].ToString()) + 1;
            //Session["item"] = btnhandsetImage.AlternateText;
            //Session["itemurl"] = btnhandsetImage.ImageUrl;
            //Session["itemplan"] = btnplanImage.AlternateText;
            //Session["planurl"] = btnplanImage.ImageUrl;
            Response.Redirect("BuyDetail.aspx");
        }
    }
}
